<div class="layuimini-main">

    <div class="layui-form layuimini-form">
        <div class="layui-form-item">
            <label class="layui-form-label required">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="username" lay-verify="required" lay-reqtext="用户名不能为空" placeholder="请输入用户名" value="" class="layui-input">
                <tip>填写自己要创建的账号，请不要使用纯数字组成的账号！</tip>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">密码</label>
            <div class="layui-input-block">
                <input type="password" name="password" lay-verify="required" lay-reqtext="密码不能为空" placeholder="请输入密码" value="" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">姓名</label>
            <div class="layui-input-block">
                <input type="text" name="fullName" lay-verify="required" lay-reqtext="姓名不能为空" placeholder="请输入姓名" value="" class="layui-input">
                <tip>姓名是为了方便分辨账号身份，请规范填写</tip>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">学院</label>
            <div class="layui-input-block">
                <select name="college" lay-verify="" id="college-select" lay-filter="college-select">
                    <option value="">请选择一个学院</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">专业</label>
            <div class="layui-input-block">
                <select name="major" lay-verify=""  id="major-select" lay-filter="major-select">
                    <option value="">需要先选择学院</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['form', 'table'], function () {
        var form = layui.form,
            layer = layui.layer,
            table = layui.table,
            $ = layui.$;

        //获取学院
        jq1111.ajax({
            type: "GET",
            async:false,
            crossDomain:true,
            url: BasePath+"api/School/Colleges",
            contentType: "application/json",
            dataType:"json",
            xhrFields: {
                withCredentials: true
            },
            success:function (message) {
                var colleges=message.data;
                console.log(message);
                colleges.forEach(function (college) {
                    console.log(college)
                    jq1111("#college-select").append('' +
                        '<option value="' +
                        college.id +
                        '">' +
                        college.name +
                        '</option>' +
                        '')
                })
                //再次初始化form
                form.render();
            },
            error:function (message) {
                console.log(message.responseJSON);
                layer.msg('失败:'+message.responseJSON.message);
            },
        });

        //监听学院的select事件来获取专业
        form.on('select(college-select)', function(data){
            console.log(data)
            if(data.value == ""){
                jq1111("#major-select").attr("disabled","disabled");
                jq1111("#class-select").attr("disabled","disabled");
                form.render('select');
            }else{
                jq1111("#major-select").removeAttr("disabled");
                //移除之前添加的专业
                var children=jq1111("#major-select").children();
                for (i=1;i<children.length;i++){
                    children.eq(i).remove();
                }
                //获取专业
                jq1111.ajax({
                    type: "GET",
                    async:false,
                    crossDomain:true,
                    url: BasePath+"api/School/Majors?collegeId="+data.value,
                    contentType: "application/json",
                    dataType:"json",
                    xhrFields: {
                        withCredentials: true
                    },
                    success:function (message) {
                        var majors=message.data;
                        majors.forEach(function (major) {
                            jq1111("#major-select").append('' +
                                '<option value="' +
                                major.id +
                                '">' +
                                major.name +
                                '</option>' +
                                '')
                        });
                        form.render('select');
                    },
                    error:function (message) {
                        console.log(message.responseJSON);
                    },
                });
                form.render('select');//select是固定写法 不是选择器
            }
        });


        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();

        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;

        //监听提交
        form.on('submit(saveBtn)', function (data) {

            jq1111.ajax({
                type: "POST",
                crossDomain:true,
                url: BasePath+"api/Account/Instructor/Create",
                contentType: "application/json",
                dataType:"json",
                xhrFields: {
                    withCredentials: true
                },
                data:JSON.stringify({
                    "username":data.field.username,
                    "password":data.field.password,
                    "managedMajorId": data.field.major,
                    "fullName": data.field.fullName
                }),
                success:function (message) {
                    console.log(message);
                    var index = layer.alert(message.msg, {
                        title: '操作结果'
                    }, function () {
                        // 关闭弹出层
                        layer.close(index);
                        layer.close(parentIndex);
                    });
                },
                error:function (message) {
                    console.log(message.responseJSON);
                    layer.msg('登录失败:'+message.responseJSON.msg);
                },
            });
            return false;
        });

    });
</script>